package 代码随想录.链表.反转链表;

/**lc206. 反转链表
 * link: https://leetcode-cn.com/problems/reverse-linked-list/
 * @author ChenweiLin
 * @create 2021-12-02 20:36
 */
public class reverseLinkedList {
    public static void main(String[] args) {

    }
    class ListNode {
          int val;
          ListNode next;
          ListNode() {}
          ListNode(int val) { this.val = val; }
      }


    public ListNode reverseList(ListNode head) {
        //因为要协助原链表第一个节点的反转
        ListNode wei = null;
        //当前节点，从wei开始，因为要反转
        //协助反转的节点
        ListNode temp = null;
        while (head != null){
            //step:依次反转 每个循环依次反转
            temp = head.next;//使用temp暂存剩余链表
            head.next = wei; //局部反转
            wei = head;//wei来到当前刚经历指针反转的节点
            head = temp;//head继承原来的剩余链表

        }

        return wei;
    }



    }
